﻿
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using Utils;
using PanasonicWarranty.DAL;
namespace PanasonicWarranty.Business
{
    public partial class SysGroup
    {
    	#region public properties
		[Required(ErrorMessage="GroupID Required")]
		public int GroupID { get; set; }
		[Required(ErrorMessage="Group Code Required")]
		public string GroupCode { get; set; }
		[Required(ErrorMessage="Group Name Required")]
		public string GroupName { get; set; }
		public string Description { get; set; }
		public int? Ord { get; set; }
		[Required(ErrorMessage="Disabled Required")]
		public bool Disabled { get; set; }
		#endregion

		#region constructor
		public SysGroup(){ }

		public SysGroup(IDataReader rd)
		{
			this.GroupID = (int)rd["GroupID"];
			this.GroupCode = rd["GroupCode"].ToString();
			this.GroupName = rd["GroupName"].ToString();
			this.Description = rd["Description"].ToString();
			this.Ord = rd["Ord"].Equals(DBNull.Value) ? (int?)null : (int)rd["Ord"];
			this.Disabled = (bool)rd["Disabled"];
		}
		#endregion

		#region private method
        private bool Create()
        {
            return DBSysGroup.Create(GroupCode, GroupName, Description, Ord, Disabled);
        }

        private bool Update()
        {
            return DBSysGroup.Update(GroupID, GroupCode, GroupName, Description, Ord, Disabled);
        }
		#endregion

		#region public method
		public bool Save()
		{
            if(GroupID != 0)
            {
                return Update();
            }
            else
            {
                return Create();
            }
		}
		#endregion

		#region static method
        public static bool Delete(int GroupID)
        {
            return DBSysGroup.Delete(GroupID);
        }

        public static List<SysGroup> GetAll()
        {
            return ObjectClass.CreateList<SysGroup>(DBSysGroup.GetAll());
        }

        public static SysGroup GetByKey(int GroupID)
        {
            return ObjectClass.CreateInstance<SysGroup>(DBSysGroup.GetByKey(GroupID));
        }

        public static List<SysGroup> GetByTop(string top, string where, string order)
        {
            return ObjectClass.CreateList<SysGroup>(DBSysGroup.GetByTop(top, where, order));
        }
		#endregion
	}
}
